package org.example;

import org.iq80.leveldb.DBException;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.impl.DbImpl;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * 简化字符串操作
 */
public class StringDB extends DbImpl {

    public static final String CHAR_SET = "UTF-8";

    public StringDB(Options options, File databaseDir) throws IOException {
        super(options, databaseDir);
    }

    public StringDB(String dir) throws IOException {
        this(new Options(),new File(dir));
    }

    public void put(String key, String value) throws DBException {
        try {
            put(key.getBytes(CHAR_SET),value.getBytes(CHAR_SET));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public String get(String key) throws DBException {
        try {
            return new String(get(key.getBytes(CHAR_SET)),CHAR_SET);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
